'''
    专门和数据库进行交互
'''
from mysqlConnect import connect
def login1(username, password):
    '''
        登录
    '''
    select_sql =  'select * from userinfo '\
                  'where username = "{username}" and password = "{password}"'\
                  .format(username=username, password=password)

    conn, cursor = connect()
    cursor.execute(select_sql)
    result = cursor.fetchall()
    if result:
        for row in result:
            update_user_bool_sql = 'update userinfo set is_login = 1 '\
                                   'where id = {id}'.format(id=row[0])
            cursor.execute(update_user_bool_sql)
            conn.commit()
    conn.close()
    return result

def register1(user):
    '''
        注册
    '''
    insert_sql = 'insert into userinfo(username, password, age, gender) '\
                 'values("{username}", "{password}", {age}, "{gender}")'\
                 .format(username=user.username, password=user.password, age=user.age, gender=user.gender)
    conn, cursor = connect()
    result = cursor.execute(insert_sql)
    conn.commit()
    conn.close()
    return result

def logoff1(username):
    '''
        注销
    '''
    update_user_bool_sql = 'delete from userinfo where username = "{username}"'.format(username=username)
    conn, cursor = connect()
    cursor.execute(update_user_bool_sql)
    conn.commit()
    conn.close()